<template>
  <div class="loadHomeWork">
    <div class="loadHomeWorkIneer">
      <div class="loadTop">系统设置</div>
      <div class="loadAndSearch">
        <div class="loadExcel">
          <div class="button" @click="addOne()">新增</div>
          <div class="buttonBack" @click="backToIndex()">返回</div>
        </div>
        <div class="search">
          <input
            type="text"
            placeholder="名称"
            v-model="fillPeople"
            @input="changeText()"
          />
          <div class="searchButton" @click="getRenderList()">搜索</div>
        </div>
      </div>
      <div class="table" :style="{ width: tableWidth }">
        <el-table
          v-if="dicTypeTypeId != 1"
          empty-text="暂无数据"
          :header-cell-style="{
            background: '#FAFAFA',
            color: '#777676 !important',
            fontWeight: '800',
          }"
          :data="tableData"
          border
          stripe
          style="width: 100%; height: auto"
          max-height="65vh"
          :row-style="{ height: 30 + 'px' }"
          :cell-style="{ padding: 0 + 'px' }"
          @row-dblclick="handleRowDblclick"
        >
          <el-table-column
            label="序号"
            align="center"
            type="index"
            :index="customIndex"
            width="50"
          />
          <el-table-column
            prop="name"
            label="名称"
            align="center"
            min-width="110"
          />
        </el-table>
        <el-table
          v-else-if="dicTypeTypeId == 1"
          empty-text="暂无数据"
          :header-cell-style="{
            background: '#FAFAFA',
            color: '#777676 !important',
            fontWeight: '800',
          }"
          :data="tableData"
          border
          stripe
          style="width: 100%; height: auto"
          max-height="65vh"
          :row-style="{ height: 30 + 'px' }"
          :cell-style="{ padding: 0 + 'px' }"
          @row-dblclick="handleRowDblclick"
        >
          <el-table-column
            label="序号"
            align="center"
            type="index"
            :index="customIndex"
            width="50"
          />
          <el-table-column
            prop="name"
            label="名称"
            align="center"
            min-width="110"
          />
          <el-table-column
            prop="name"
            label="船图片"
            align="center"
            min-width="110"
          >
            <template #default="scope">
              <div class="logoImage">
                <img :src="imageSrc(scope.row.image)" alt="" />
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- 内容过多，会出现滚动条 -->
      </div>
      <div class="foot">
        <div class="currentPage">当前第{{ page }}/{{ totalPages }}页</div>
        <div
          class="lastPage"
          :style="{ background: color }"
          @click="clickLastPage()"
        >
          上一页
        </div>
        <div
          class="nextPage"
          :style="{ backgroundColor: color1 }"
          @click="clickNextPage()"
        >
          下一页
        </div>
        <div class="currentNumber">共{{ totalNumber }}条</div>
        <div>跳转至:</div>
        <input
          type="text"
          class="skipPage"
          style="width: 50px !important"
          v-model="shipPageNumer"
        />
        <div class="sureSkip" @click="skipPage">确定</div>
      </div>
    </div>
    <div class="dialog" ref="dialog" :style="{ display: show }">
      <div class="loadHomeWorkIneer">
        <div class="changeTop">
          <div
            class="delete"
            @click="deleteOne(id)"
            v-show="addOrEdit == '编辑'"
          >
            删除
          </div>
          <div class="sure" @click="sureAdd()">确认</div>
          <div class="back" @click="justBack()">返回</div>
        </div>
        <div class="dialogText">{{ addOrEdit }}</div>
        <div class="manyInput">
          <div class="iuptInnerContain">
            <div class="inputInner">
              <div class="name">名称:</div>
              <input type="text" placeholder="名称" v-model="name" />
            </div>

            <div
              class="inputInner1"
              v-show="dicTypeTypeId == 1"
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <div
                class="name1"
                v-show="this.imageFlage == false"
                @click="showUpload"
              >
                上传船图片
              </div>
              <div
                class="name1"
                v-show="this.imageFlage == true"
                @click="showUpload"
              >
                修改船图片
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="dialog2" :style="{ display: uploadfile }">
      <div class="upContain">
        <input
          type="file"
          ref="clearFile"
          class="addfile"
          @change="getFile($event)"
          accept="image/*"
        />
      </div>
      <div class="fileName">
        <div v-show="fileName">文件名:{{ fileName }}</div>
      </div>
      <div class="upFileSrue">
        <button class="buttonUpdata" @click="cancelUp()">取消</button>
        <button class="buttonUpdata buttonUpdata1" @click="submitAddFile()">
          确定
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import EventBus from "@/mitt/eventBus.js";
import rename from "@/mitt/getPath.js";
import service from "@/api/api";
import _ from "lodash";
import zhCn from "element-plus/es/locale/lang/zh-cn";
import jsencrypt from "jsencrypt";

export default {
  name: "",
  data() {
    return {
      compare: {},
      shipPageNumer: 1,
      jsencrypt: "",
      locale: zhCn,
      value1: "",
      tableData: [],
      tableWidth: null,
      page: 1,
      size: 20,
      pageList: [],
      show: "none",
      addOrEdit: "新增",
      currentNum: 0,
      roleList: [],
      totalNumber: 0,
      name: "",
      value: "",
      fillPeople: "",
      id: "",
      uploadfile: "none",
      totalPages: null,
      image: "",
      oldimage: "",
      data: {
        name: "",
        value: "",
        dicTypeTypeId: "",
        id: "",
      },

      show1: "none",
      color: "#EBEBEB",
      color1: "#EBEBEB",
      file: null,
      fileName: "",
      jtData: null,
      multi: [],
      imageFlage: false,
    };
  },
  props: [],
  components: {},
  computed: {},
  filters: {},
  methods: {
    // 选择图片文件
    backToIndex() {
      this.$router.push("/app.dictDataSee");
    },
    imageSrc(src) {
      if (src) {
        return `data:mage/png;base64,${src}`;
      } else return "";
    },
    showUpload() {
      this.uploadfile = "block";
    },
    submitAddFile() {
      this.$message({
        message: "船图片选择成功",
        type: "success",
      });
      this.uploadfile = "none";
      this.fileName = "";
      document.getElementsByClassName("addfile")[0].value = "";
    },
    cancelUp() {
      this.file = "";
      this.uploadfile = "none";
      this.fileName = "";
      this.image = this.oldimage;
      document.getElementsByClassName("addfile")[0].value = "";
    },
    async getFile(event) {
      const file = event.target.files[0];
      if (!file) return;
      if (file.size > 1000000) {
        this.file = "";
        // this.uploadfile = "none";
        this.fileName = "";

        document.getElementsByClassName("addfile")[0].value = "";
        return this.$message({
          message: "图片文件不能超过1M",
          type: "warning",
        });
      }
      this.fileName = file.name;

      const reader = new FileReader();
      reader.onload = (event) => {
        const base64String = event.target.result;
        const pureBase64 = base64String.split(",")[1];
        this.image = pureBase64;
      };
      reader.readAsDataURL(file);
    },

    handleRowDblclick(row) {
      this.clickEdit(row.id);
    },

    skipPage() {
      if (this.shipPageNumer <= this.totalPages && this.shipPageNumer > 0) {
        this.page = this.shipPageNumer;
        this.getRenderList();
      } else {
        this.$message({
          type: "warning",
          message: "超出页面数量范围",
        });
      }
    },

    customIndex(index) {
      return (this.page - 1) * this.size + index + 1;
    },
    justBack() {
      if (this.addOrEdit == "编辑") {
        let data = {};
        if (this.dicTypeTypeId == 1) {
          data = {
            name: this.name,
            value: this.value,
            image: this.image,
          };
        } else {
          data = {
            name: this.name,
            value: this.value,
          };
        }

        let equal = _.isEqual(data, JSON.parse(this.compare));

        if (equal) {
          this.close();
        } else {
          this.$confirm("数据信息有变化是否保存再离开？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            // type: "warning",
          })
            .then(() => {
              this.sureAdd();
            })
            .catch(() => {
              this.close();
            });
        }
      } else if (this.addOrEdit == "新增") {
        this.close();
      }
    },
    async clickEdit(a) {
      this.addOrEdit = "编辑";
      this.tableData.forEach((item) => {
        if (item.id == a) {
          this.id = item.id;
          this.name = item.name;
          this.value = item.value;
          this.compare = JSON.stringify({
            name: item.name,
            value: item.value,
          });
          if (this.dicTypeTypeId == 1) {
            try {
              if (item.image) {
                this.oldimage = item.image;
                this.imageFlage = true;
              } else {
                this.imageFlage = false;
              }
              this.compare = JSON.stringify({
                name: item.name,
                value: item.value,
                image: item.image,
              });
            } catch (error) {}
          }
        }
      });
      this.show = "block";
    },
    changeText() {
      this.page = 1;
    },
    // 删除数据
    deleteOne(a) {
      // let that = this;
      this.$alert("确定删除这条信息？", {
        confirmButtonText: "确定",
        callback: (action) => {

          if (action == "confirm") {
            service({
              method: "delete",
              url: `/dict-item/${a}`,
            })
              .then((r) => {

                if (r.status == "SUCCESS") {
                  this.$message({
                    type: "success",
                    message: "删除成功",
                  });
                  if (this.currentNum == 1 && this.page > 1) {
                    this.page--;
                  }
                  this.getRenderList();
                  this.show = "none";
                } else {
                  this.$message({
                    type: "info",
                    message: r.error,
                  });
                }
              })
              .catch((err) => {
                this.$message({
                  type: "danger",
                  message: "出错了！",
                });
              });
          } else {
            this.$message({
              type: "info",
              message: "取消删除",
            });
          }
        },
      });
    },

    sureAdd() {
      if (this.dicTypeTypeId == 1) {
        this.data = {
          name: this.name,

          dicTypeTypeId: this.dicTypeTypeId,
          image: this.image,
        };
        if (this.addOrEdit == "编辑") {
          this.data.id = this.id;
          service({
            method: "put",
            data: this.data,
            url: "/dict-item",
          })
            .then((r) => {
              if (r.status == "SUCCESS") {
                this.$message({
                  type: "success",
                  message: "修改成功",
                });
                this.getRenderList();
                this.show = "none";
                this.clearCanShu();
              } else {
                this.$message({
                  type: "info",
                  message: r.error,
                });
              }
            })
            .catch((err) => {
              this.$message({
                type: "info",
                message: "出错了",
              });
            });
        } else {
          service({
            method: "post",
            data: this.data,
            url: "/dict-item",
          })
            .then((r) => {

              if (r.status == "SUCCESS") {
                this.$message({
                  type: "success",
                  message: "新增成功",
                });
                this.getRenderList();
                this.show = "none";
                this.clearCanShu();
              }
            })
            .catch((err) => {
              this.$message({
                type: "info",
                message: "出错了！",
              });
            });
        }
      } else {
        this.data.name = this.name;

        this.data.dicTypeTypeId = this.dicTypeTypeId;

        if (this.addOrEdit == "编辑") {
          this.data.id = this.id;
          service({
            method: "put",
            data: {
              name: this.data.name,
              id: this.data.id,
              dicTypeTypeId: this.dicTypeTypeId,
            },
            url: "/dict-item",
          })
            .then((r) => {
              if (r.status == "SUCCESS") {
                this.$message({
                  type: "success",
                  message: "修改成功",
                });
                this.getRenderList();
                this.show = "none";
                this.clearCanShu();
              } else {
                this.$message({
                  type: "info",
                  message: r.error,
                });
              }
            })
            .catch((err) => {
              this.$message({
                type: "info",
                message: "出错了",
              });
            });
        } else {

          // return;
          service({
            method: "post",
            data: { name: this.data.name, dicTypeTypeId: this.dicTypeTypeId },
            url: "/dict-item",
          })
            .then((r) => {

              if (r.status == "SUCCESS") {
                this.$message({
                  type: "success",
                  message: "新增成功",
                });
                this.getRenderList();
                this.show = "none";
                this.clearCanShu();
              }
            })
            .catch((err) => {
              this.$message({
                type: "info",
                message: "出错了！",
              });
            });
        }
      }
    },

    close() {
      this.show = "none";
      this.show1 = "none";
      this.clearCanShu();
    },
    // 新增
    addOne() {
      this.addOrEdit = "新增";
      // this.$refs["dialog"].style.display = show;
      this.imageFlage = false;
      this.show = "block";
    },
    // 导出文件

    sendMessage() {
      let res = rename();
      EventBus.emit("custom-event", res);
    },

    //单个角色

    // 获取列表数据
    async getRenderList() {
      let data = {};
      if (this.fillPeople == "") {
        data = {
          page: this.page,
          size: this.size,
          dicTypeTypeId: this.dicTypeTypeId,
        };
      } else if (this.fillPeople) {
        data = {
          page: this.page,
          size: this.size,
          name: this.fillPeople,
        };
      }

      await service({
        methods: "get",
        url: "/dict-item/find-one-page",
        params: data,
      })
        .then((e) => {
          this.totalPages = e.data.totalPages;
          this.totalNumber = e.data.totalElements;
          if (this.totalPages == 0) {
            this.totalPages = 1;
          }
          if (this.page < this.totalPages) {
            this.color1 = "#268FDE";
          } else {
            this.color1 = "#EBEBEB";
          }
          if (this.page == 1) {
            this.color = "#EBEBEB";
          } else {
            this.color = "#268FDE";
          }
          this.tableData = e.data.content;
          this.currentNum = e.data.numberOfElements;
        })
        .catch((err) => {});
    },

    clearCanShu() {
      this.name = "";
      this.value = "";
      this.id = "";

      this.data = {
        name: "",
        value: "",
        id: "",
      };
    },

    clickLastPage() {
      if (this.page > 1) {
        this.page--;
        if (this.page == 1) {
          this.color = "#EBEBEB";
        } else {
          this.color = "#268FDE";
        }
        if (this.page < this.totalPages) {
          this.color1 = "#268FDE";
        }
        this.getRenderList();
      } else {
        this.color = "#EBEBEB";
      }
    },
    clickNextPage() {
      if (this.page < this.totalPages) {
        this.page++;
        this.color = "#268FDE";
        if (this.page == this.totalPages) {
          this.color1 = "#EBEBEB";
        } else {
          this.color1 = "#268FDE";
        }
      }
      this.getRenderList();
    },
    handSize() {
      if (window.innerWidth > 630) {
        if (this.$store.state.width == 200) {
          this.tableWidth = window.innerWidth - 220 + "px";
        } else if (this.$store.state.width == 90) {
          this.tableWidth = window.innerWidth - 100 + "px";
        }
      } else {
        this.tableWidth = window.innerWidth - 20 + "px";
      }
    },
  },
  watch: {
    "$store.state.width"(newVal, oldVal) {
      //对数据执行操作

      if (window.innerWidth > 630) {
        if (newVal == 90) {
          this.tableWidth = (window.innerWidth - 60) * 0.985 - 20 + "px";
        } else if (newVal == 200) {
          this.tableWidth = (window.innerWidth - 170) * 0.985 - 20 + "px";
        }
      }
       
    },
  },

  created() {
    this.sendMessage();

    this.dicTypeTypeId = JSON.parse(this.$route.query.id);

  },
  beforeMount() {
    if (window.innerWidth > 630) {
      if (this.$store.state.width == 200) {
        this.tableWidth = (window.innerWidth - 170) * 0.985 - 20 + "px";
      } else if (this.$store.state.width == 90) {
        this.tableWidth = (window.innerWidth - 60) * 0.985 - 20 + "px";
      }
    } else {
      this.tableWidth = window.innerWidth - 30 + "px";
    }
  },
  mounted() {
    let that = this;
    this.getRenderList();

    // this.sendMessage();
    window.addEventListener("resize", this.handSize);
  },
  beforeUnmount() {
    window.removeEventListener("resize", this.handSize);
  },
};
</script>
<style lang="less" scoped>
.addfile {
  width: 100%;
  height: 40px;
  font-size: 15px;
}
.addfile::file-selector-button {
  width: 100%;
  height: 4vh;
  background-color: #1e9fff;
  border: 1px solid #1e9fff;
  border-radius: 3px;
  cursor: pointer;
  color: #fff;
  font-size: 15px;
}
.fileName {
  font-size: 15px;
  color: black;
  height: 3vh;
  line-height: 3vh;
  width: 100%;
  text-align: center;
}

.manyInput {
  width: 780px;
  height: 80vh;
  overflow: auto;
  // background: red;
  margin: auto;

  // overflow: scroll;
  .iuptInnerContain {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    .inputInner {
      width: 375px;
      height: 35px;
      // background: blue;
      margin-right: 10px;
      margin-top: 10px;
      display: flex;
      align-items: center;
      // justify-content: space-between;
      color: #365488;
      .name {
        width: 150px;
        height: 100%;
        line-height: 4vh;
        text-align: right;
        font-size: 15px;
      }
      .name1 {
        width: 150px;
        height: 100%;
        height: 3vh;
        line-height: 3vh;
        justify-content: center;
        outline: none;
        font-size: 15px;
        border: none;
        background: #5cc735;
        color: white;
      }
      .inputContainer {
        width: 210px;
        margin-left: 5px;
      }
      .el-input__inner {
        background: white !important;
      }
      input {
        width: 220px;
        height: 34px;
        margin-left: 5px;
        border-radius: 5px;
        text-indent: 1em;
        outline: none;
        border: none;
        background: white !important;
        color: #365488;
        border: 1px solid #dcdfe6;
      }
      input::-webkit-input-placeholder {
        color: #a8abb2;
      }
      input[type="date"] {
        // padding: 5px;
        color: #365488;
        text-align: left;
        // border: 1px solid #365488;
        border-radius: 4px;
        // background-color: #fff;
        box-sizing: border-box; /* 确保内边距和边框不会影响元素的总宽度 */
      }
      // input[type="number"] {
      //   appearance: textfield;
      //   -moz-appearance: textfield;
      // }
      // 　　input[type="number"]::-webkit-inner-spin-button,
      // 　　input[type="number"]::-webkit-outer-spin-button {
      //   -webkit-appearance: none;
      //   margin: 0;
      // }
    }
    .inputInner1 {
      width: 150px;
      height: 35px;
      margin: auto;
      margin-top: 10px;
      .name1 {
        width: 150px;
        height: 100%;
        height: 3vh;
        line-height: 3vh;
        justify-content: center;
        outline: none;
        font-size: 15px;
        border: none;
        background: #5cc735;
        color: white;
        text-align: center;
      }
    }
    .el-date-picker .el-picker-panel {
      background-color: white !important; /* 你想要的背景颜色 */
    }
  }
}
.dialogText {
  margin: auto;
  width: 50%;
  line-height: 4vh;
  font-size: 25px;
  color: black;
  // text-indent: 10px;
  text-align: center;
  // font-size: 20px;
  font-weight: 800;
}

.dialog,
.dialog1 {
  width: 100%;
  height: 100%;
  background: #dfe6e8;
  position: absolute;
  top: 0;
  left: 0;
  display: none;
  z-index: 100;
}

.loadHomeWork {
  overflow: hidden;
  width: 100%;
  height: 100%;
  position: relative;
  .loadHomeWorkIneer {
    width: 98.5%;
    height: 91vh;
    margin: auto;
    margin-top: 1vh;
    background: white;
    .loadTop {
      width: 50%;
      line-height: 4vh;
      color: #777676;
      // text-indent: 10px;
      margin-left: 10px;
      font-size: 20px;
      font-weight: 800;
    }
    .loadSecond {
      width: 280px;

      color: #777676;
      text-align: center;
      line-height: 33px;

      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-left: 10px;
      font-size: 15px;
      .topName {
        width: 90px;
        line-height: 33px;
        background: #67c23a;
        border-radius: 5px;
        a {
          display: block;
          width: 90px;
          line-height: 33px;
          text-decoration: none;
          color: white;
        }
      }
      .topName:nth-child(3) {
        background: #242121;
      }
    }
  }
  // background: blue;
}
.table {
  width: 98.5%;
  margin: auto;
  // margin-top: 2vh;
  height: 65vh;
  // background: red;
}

/* 必须是input为 checkbox class 添加 switch 才能实现以下效果 */

input[type="checkbox"].switch {
  outline: none;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  position: relative;
  width: 40px;
  margin-top: 5px;
  height: 20px;
  background: #ccc;
  border-radius: 10px;
  transition: border-color 0.3s, background-color 0.3s;
}

input[type="checkbox"].switch::after {
  content: "";
  display: inline-block;
  width: 1rem;
  height: 1rem;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0, 0, 2px, #999;
  transition: 0.4s;
  top: 2px;
  position: absolute;
  left: 2px;
}

input[type="checkbox"].switch:checked {
  background: rgb(19, 206, 102);
}
/* 当input[type=checkbox]被选中时：伪元素显示下面样式 位置发生变化 */
input[type="checkbox"].switch:checked::after {
  content: "";
  position: absolute;
  left: 55%;
  top: 2px;
}

@media (min-width: 0px) and (max-width: 960px) {
  .manyInput {
    width: 375px !important;
    display: block !important;
    .inputInner {
      margin: auto !important;
      // margin-top: 10px !important;
    }
  }
  .closeAndSrue {
    width: 110px;
  }
}
</style>
